package com.atguigu.tingshu.order.strategy.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.IdUtil;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.order.helper.SignHelper;
import com.atguigu.tingshu.order.strategy.TradeDataStrategy;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component("1003")
@Slf4j
public class VIPTradeDataStrategy implements TradeDataStrategy {
    @Autowired
    private UserFeignClient userFeignClient;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void handlerTradeData(TradeVo tradeVo, OrderInfoVo orderInfoVo) {

        //1.1 初始化订单结算页三个价格：原价、减免价、订单价
        BigDecimal originalAmount = new BigDecimal("0.00");
        BigDecimal derateAmount = new BigDecimal("0.00");
        BigDecimal orderAmount = new BigDecimal("0.00");

        //1.2 初始化订单结算页两个集合：商品明细、商品优惠集合
        List<OrderDetailVo> orderDetailVoList = new ArrayList<>();
        List<OrderDerateVo> orderDerateVoList = new ArrayList<>();

        //2.1远程调用用户服务获取套餐详情
        VipServiceConfig vipServiceConfig = userFeignClient.getVipServiceConfig(tradeVo.getItemId()).getData();
        Assert.notNull(vipServiceConfig, "套餐{}不存在",tradeVo.getItemId());
        //计算订单相关价格
        originalAmount = vipServiceConfig.getPrice();
        orderAmount = vipServiceConfig.getDiscountPrice();
        if (orderAmount.compareTo(originalAmount) != 0) {
            //减免价格=原价-订单价
            derateAmount = originalAmount.subtract(orderAmount);
        }
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVo.setItemId(tradeVo.getItemId());
        orderDetailVo.setItemName("会员：" + vipServiceConfig.getName());
        orderDetailVo.setItemPrice(originalAmount);
        orderDetailVo.setItemUrl(vipServiceConfig.getImageUrl());
        orderDetailVoList.add(orderDetailVo);
        //封装订单减免明细
        if (orderAmount.compareTo(originalAmount) != 0) {
            OrderDerateVo orderDerateVo = new OrderDerateVo();
            orderDerateVo.setDerateType(SystemConstant.ORDER_DERATE_VIP_SERVICE_DISCOUNT);
            orderDerateVo.setDerateAmount(derateAmount);
            orderDerateVo.setRemarks("会员限时优惠：" + derateAmount);
            orderDerateVoList.add(orderDerateVo);
        }
        orderInfoVo.setOriginalAmount(originalAmount);
        orderInfoVo.setDerateAmount(derateAmount);
        orderInfoVo.setOrderAmount(orderAmount);
        orderInfoVo.setOrderDetailVoList(orderDetailVoList);
        orderInfoVo.setOrderDerateVoList(orderDerateVoList);
    }
}
